package com.rfs.thread;

import javax.sound.midi.Soundbank;
import java.util.concurrent.TimeUnit;

/**
 * @author renfushuai
 * @date 2022/6/6
 * 线程中断
 * 抛出InterruptedException的线程SleepThread，其中断标识位被清除了，而一直忙碌运作的线程BusyThread，中断标识位没有被清除。
 */
public class Interrupted {
    public static void main(String[] args) throws InterruptedException {
        // SleepThread不停的尝试睡眠
        Thread sleepThread = new Thread(() -> {
            while (true) {
                try {
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "SleepThread");
        sleepThread.setDaemon(true);
        Thread busyThread = new Thread(() -> {
            while (true) {

            }
        }, "BusyThread");
        busyThread.setDaemon(true);
        sleepThread.start();
        busyThread.start();
        TimeUnit.SECONDS.sleep(5);
        sleepThread.interrupt();
        busyThread.interrupt();
        System.out.println("SleepThread interrupted is "+sleepThread.isInterrupted());
        System.out.println("BusyThread interrupted is "+busyThread.isInterrupted());
        TimeUnit.SECONDS.sleep(2);
    }


}
